<?php

if (!defined('IN_XLP')) {
    exit('Access Denied!');
}

/**
 * Description of exchangeAction 积分兑换
 *
 * @author xlp
 */
class exchangeAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
        $this->status = array(1 => '上线', 0 => '<span class="red">下线</span>');
        $this->useStatus = array(1 => '已使用', 0 => '<span class="red">未使用</span>');
    }

    function add() {
        $this->assign(array('status' => $this->status));
        $this->display();
    }

    function edit() {
        $id = $this->_getid('id');
        if (!$id) {
            showError('内容不存在');
        }
        $rs = M('exchange')->where(array('id' => $id, 'aid' => admin::$adminInfo['aid']))->find();
        if (!$rs) {
            showError('内容不存在');
        }
        $this->assign(array('rs' => $rs, 'status' => $this->status));
        $this->display();
    }

    function post() {
        $objData = array();
        $fields = array(
            'aid' => admin::$adminInfo['aid'],
            'title' => '',
            'img_url' => '',
            'price' => 0,
            'units' => '',
            'counts' => 0,
            'single_counts' => 0,
            'stime' => 0,
            'etime' => 0,
            'out_time' => 0,
            'content' => '',
            'total_people' => 0,
            'infotime' => TIME,
            'status' => 0
        );
        foreach ($fields as $key => $val) {
            $objData[$key] = $this->_post($key, $val);
        }
        if ($objData['price']) {
            $objData['price'] = intval($objData['price']);
        } else {
            $this->JsonReturn('请输入每份所需积分');
        }
        if ($objData['counts']) {
            $objData['counts'] = intval($objData['counts']);
        } else {
            $this->JsonReturn('请输入礼品总数量');
        }
        if ($objData['single_counts']) {
            $objData['single_counts'] = intval($objData['single_counts']);
        }
        if (!$objData['title']) {
            $this->JsonReturn('抱歉，请输入标题');
        }
        if (!$objData['price']) {
            $this->JsonReturn('抱歉，请上传图片');
        }
        if ($objData['stime']) {
            $objData['stime'] = inTime($objData['stime']);
        }
        if ($objData['etime']) {
            $objData['etime'] = inTime($objData['etime']);
        }
        if ($objData['out_time']) {
            $objData['out_time'] = inTime($objData['out_time']);
        }
//        M('tuan')->setShowError();
        if (M('exchange')->insert($objData)) {
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('抱歉，添加失败');
        }
    }

    function save() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        $objData = array();
        $fields = array(
            'aid' => admin::$adminInfo['aid'],
            'title' => '',
            'img_url' => '',
            'price' => 0,
            'units' => '',
            'counts' => 0,
            'single_counts' => 0,
            'stime' => 0,
            'etime' => 0,
            'out_time' => 0,
            'content' => '',
            'infotime' => TIME,
            'status' => 0
        );
        foreach ($fields as $key => $val) {
            $objData[$key] = $this->_post($key, $val);
        }
        if ($objData['price']) {
            $objData['price'] = intval($objData['price']);
        } else {
            $this->JsonReturn('请输入每份所需积分');
        }
        if ($objData['counts']) {
            $objData['counts'] = intval($objData['counts']);
        } else {
            $this->JsonReturn('请输入礼品总数量');
        }
        if ($objData['single_counts']) {
            $objData['single_counts'] = intval($objData['single_counts']);
        }
        if (!$objData['title']) {
            $this->JsonReturn('抱歉，请输入标题');
        }
        if (!$objData['price']) {
            $this->JsonReturn('抱歉，请上传图片');
        }
        if ($objData['stime']) {
            $objData['stime'] = inTime($objData['stime']);
        }
        if ($objData['etime']) {
            $objData['etime'] = inTime($objData['etime']);
        }
        if ($objData['out_time']) {
            $objData['out_time'] = inTime($objData['out_time']);
        }
        if (M('exchange')->update($objData, array('id' => $id, 'aid' => admin::$adminInfo['aid']))) {
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('抱歉，操作失败');
        }
    }

    function delete() {
        $id = $this->_postid('id');
        if (!$id) {
            showError('参数丢失');
        }
        if (M('exchange')->delete(array('id' => $id, 'aid' => admin::$adminInfo['aid']))) {
            M('exchange_user')->delete(array('eid' => $id, 'aid' => admin::$adminInfo['aid']));
            $this->JsonReturn('ok', null, 1);
        } else {
            $this->JsonReturn('抱歉，删除失败');
        }
    }

    function tlist() {
        $p = $this->_getid('p', 1);
        $id = $this->_getid('id', 0);
        $status = $this->_getid('status', 0);
        $q = $this->_get('q', '');
        $where = 'a.aid=' . admin::$adminInfo['aid'] . " AND a.eid=" . $id;
        if ($q) {
            $where.=" AND a.code LIKE '%" . parent::safeSearch($q) . "%'";
        }
        if ($status) {
            $where.=' AND a.status=' . ($status - 1);
        }
        $pageShow = 20;
        $limit = (($p - 1) * $pageShow) . ',' . $pageShow;
        $rs = M('exchange_user')->query("SELECT a.*,b.nickname,b.phone FROM __TABLE__ AS a LEFT JOIN __PRE__user AS b ON a.uid=b.uid WHERE " . $where . " ORDER BY infotime DESC LIMIT " . $limit);
        $total = M('exchange_user')->where(str_replace('a.', '', $where))->count();
        $this->assign(array('rs' => $rs, 'total' => $total, 'status' => $this->useStatus, 'p' => $p, 'id' => $id, 'q' => $q));
        $this->display();
    }

    function deal() {
        $id = $this->_getid('id');
        $do = $this->_get('do');
        if (!$id || !in_array($do, array('ok', 'invalid'))) {
            showError('参数有误');
        }
        $rs = M('exchange_user')->where(array('id' => $id))->find();
        if (!$rs) {
            showError('内容不存在');
        }
        if ($rs['out_time'] < TIME) {
            showError('兑换券已过期，无法兑换');
        }
        if ($do == 'ok' && $rs['status'] != 1) {
            M('exchange_user')->update(array('status' => 1, 'use_time' => TIME), array('id' => $id));
        }
        $this->JsonReturn('ok', null, 1);
    }

    function index() {
        $p = $this->_getid('p', 1);
        $q = $this->_get('q', '');
        $status = $this->_getid('status', 0);
        $where = array('aid' => admin::$adminInfo['aid']);
        if ($q) {
            $where['LIKE'] = array('title' => $q);
        }
        if ($status) {
            $where['status'] = $status - 1;
        }
        $rs = M('exchange')->field('id,title,price,counts,single_counts,stime,etime,out_time,infotime,total_people,status')->where($where)->page($p)->order('id DESC')->findAll();
        $total = M('exchange')->getTotal();
        $this->assign(array('rs' => $rs, 'total' => $total, 'status' => $this->status, 'p' => $p));
        $this->display();
    }

}
